<?php
require_once ('..'.DIRECTORY_SEPARATOR.'initialize.php');
include_once(Utils::getPath(array('shared', 'template.php')));
include_once(Utils::getPath(array('repositories', 'solutionRepository.php')));

$template = new Template('application');
$template->initialize();

$my_id = $_SESSION['authenticatedId'];

$solutionRep = new SolutionRepository();

if(isset($_GET['type']))
{
    if($_GET['type'] == 'mine')
    {
        $check_pending = false;
        $solutions = $solutionRep->findMySolutions($my_id);
        $page_title = "My solutions";
    }
    else if($_GET['type'] == 'check')
    {
        $check_pending = true;
        $solutions = $solutionRep->findUncheckedByMe($my_id);
        $page_title = "Pending solutions for my challenges";
    }
    
?>
    <h2><?php echo $page_title; ?></h2>
    <ul id="solution-filter-mode">
        <li><a href="list.php?type=mine">My solutions</a></li>
        <li><a href="list.php?type=check">Pending for my challenges</a></li>
    </ul>
<?php
    if(count($solutions) > 0)
    {
    ?>
        <table id = "list">
            <tr>
            <?php
                if($check_pending)
                {
            ?>
                <th>Author</th>
            <?php
                }
            ?>
                <th>Challenge title</th>
                <th>Date</th>
            <?php
                if(!$check_pending)
                {
            ?>
                <th>Valid</th>
                <th>Quality</th>
             <?php
                }
            ?>
                <th>View</th>
            </tr>
        <?php
            foreach($solutions as $solution)
            {
                $urlSolution = Utils::getUrl('solution/view.php?id=' . $solution['solutionid']);
        ?>
            <tr class="list-item" onclick="location.href='<?php echo $urlSolution; ?>'">
            <?php
                if($check_pending)
                {
            ?>
                <td><a href="<?php echo Utils::getUrl('user/profile.php?id=' . $solution['authorid']); ?>"><?php echo $solution['authorusername']; ?></a></td>
            <?php
                }
            ?>
                <td><a href="<?php echo Utils::getUrl('challenge/view.php?id=' . $solution['challengeid']); ?>"><?php echo $solution['challengetitle']; ?></a></td>
                <td><?php echo $solution['date']; ?></td>
                
            <?php
                if(!$check_pending)
                {
            ?>
                <td><?php echo !isset($solution['valid']) ? "Pending" : ($solution['valid'] ? "Yes" : "No") ?></td>
                <td><?php echo $solution['quality']; ?></td>
            <?php
                }
            ?>
                
                
                <td><a href="<?php echo $urlSolution; ?>">View</a></td>
            </tr>
        <?php
            }
        ?>
        </table>
    <?php
    }
    else
    {
        ?>
        <p>You have no solutions in this category.</p>
        <?php
    }
}
else
{
    Utils::goToErrorPage();
}

$template->createPage($page_title);
?>
